package vn.cent.game.cardtype.main;

import java.util.Random;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import vn.cent.game.cardtype.bc.BCBean;
import vn.cent.game.cardtype.beans.GameBean;
import vn.cent.game.cardtype.utils.GameKind;

import com.smartfoxserver.v2.api.CreateRoomSettings;
import com.smartfoxserver.v2.api.ISFSApi;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.SFSRoomRemoveMode;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.exceptions.SFSCreateRoomException;
import com.smartfoxserver.v2.exceptions.SFSJoinRoomException;
import com.smartfoxserver.v2.extensions.SFSExtension;


public class RoomController extends SFSExtension{
	
	public final String BC_ROOM_PREFIX = "Card.BC_Room";
	public final int BC_ROOM_MAX_USER = 10;
	
	private ISFSApi apiObj;
	private CardTypeGameExtension gameExtension;
	private User user;
	private int userID;
	
	
	public RoomController(ISFSApi api, CardTypeGameExtension extension, User sender)
	{
		apiObj = api;
		gameExtension = extension;
		user = sender;
		userID = user.getId();
	}
	
	public void CreatePublicBCRoom()
	{
		trace("[RoomController] Start create public BC room");
		int randomElement = new Random().nextInt(1000);
		
		CreateRoomSettings rSettings = new CreateRoomSettings();
		
		rSettings.setName(BC_ROOM_PREFIX + user.getId() + randomElement);
		rSettings.setMaxUsers(BC_ROOM_MAX_USER);
		rSettings.setGame(true);
		rSettings.setDynamic(true);
		rSettings.setAutoRemoveMode(SFSRoomRemoveMode.WHEN_EMPTY);
		
		Room room = null;
		
		try {
			room = apiObj.createRoom(gameExtension.getParentZone(), rSettings, user);
			room.setActive(true);
			
			// Push to room list 
			BCBean bcGame = new BCBean(room, userID);
			bcGame.getPlayers().put(userID, user);
			gameExtension.getListGameBC().put(room.getId(), bcGame);
			trace("[RoomController] Room BC had been created successful");
			
			try {
				apiObj.joinRoom(user, room);
			} catch (SFSJoinRoomException e) {
				trace("[RoomController] Host join room exception");
				trace(e.toString());
			}
			
		} catch (SFSCreateRoomException e) {
			trace("[RoomController] Create BC room exception");
			trace(e.toString());
		}
	}
	
	public void CreatePrivateBCRoom(String rName, String rPassword)
	{
		trace("[RoomController] Start create private BC room");
		int randomElement = new Random().nextInt(1000);
		
		CreateRoomSettings rSettings = new CreateRoomSettings();
		
		//rSettings.setName(BC_ROOM_PREFIX + user.getId() + randomElement);
		// Add those elements 
		rSettings.setName(rName);
		rSettings.setPassword(rPassword);
		// Add those elements
		rSettings.setMaxUsers(BC_ROOM_MAX_USER);
		rSettings.setGame(true);
		rSettings.setDynamic(true);
		rSettings.setAutoRemoveMode(SFSRoomRemoveMode.WHEN_EMPTY);
		
		Room room = null;
		
		try {
			room = apiObj.createRoom(gameExtension.getParentZone(), rSettings, user);
			room.setActive(true);
			
			// Push to room list 
			BCBean bcGame = new BCBean(room, userID);
			bcGame.getPlayers().put(userID, user);
			gameExtension.getListGameBC().put(room.getId(), bcGame);
			trace("[RoomController] Private room BC had been created successful");
			
			try {
				//apiObj.joinRoom(user, room);
				// Change join params
				apiObj.joinRoom(user, room, rPassword, false, user.getLastJoinedRoom());
			} catch (SFSJoinRoomException e) {
				trace("[RoomController] Host join private room exception");
				trace(e.toString());
			}
			
		} catch (SFSCreateRoomException e) {
			trace("[RoomController] Create private BC room exception");
			trace(e.toString());
		}
	}
	
	public void JoinPublicRoom(GameBean roomToJoin)
	{
		trace("[RoomController] Start try to join public BC room");
		try {
			apiObj.joinRoom(user, roomToJoin.getRoomID());

			// Set properties
			roomToJoin.setGuest(userID);
			
			roomToJoin.getPlayers().put(userID, user);
			
			roomToJoin.StartGame(gameExtension, roomToJoin.getRoomID().getPlayersList());
			
			roomToJoin.setStarted(true);
		} catch (SFSJoinRoomException e) {
			trace("[RoomController] Join BC room errors. Please check details: ");
			trace(e.toString());
		}
	}
	
	public void JoinPrivateBCRoom(GameBean roomToJoin)
	{
		
	}
	
	public void CreatePublicCARoom(User sender)
	{
		
	}
	
	public void CreatePrivateCARoom(User sender)
	{
		
	}
	
	public void JoinPrivateCARoom(GameBean roomToJoin)
	{
		
	}
	
	public GameBean FindPublicRoom(GameKind gameKind)
	{
		SortRoomPriority();
		/*
		 * Find room stragety:
		 * For each room from this game kind's room list, check if JoinPriority is high
		 * and make join if max_user > current user quantity
		 */
		switch (gameKind) {
		case None:
			{
				// Do nothing right now
				break;
			}
		case BC:
		{
			ConcurrentHashMap<Integer, BCBean> listGameBC = gameExtension.getListGameBC();
			for (Entry<Integer,BCBean> interable_element : listGameBC.entrySet())
			{
				BCBean game = interable_element.getValue();
				if (!game.isStarted() && !game.getRoomID().isPasswordProtected())
				{
					return game;
				}
			}
			break;
		}
			default:
				break;
		}
		
		// Find nothing, return null
		trace("[MakeReady] Find room got somethings wrong. Please check again!");
		return null;
	}
	
	public GameBean FindPrivateRoom(int roomID)
	{
		GameBean availableRoom = null;
		if (gameExtension.getListGameBC().containsKey(roomID)) {
			availableRoom = gameExtension.getListGameBC().get(roomID);			
		}
		return availableRoom;
	}
	
	
	// Do not use right now. But need to provide new user to room 
	// Do implement later to decrease server traffic load
	// and make room more balance
	private void SortRoomPriority()
	{
		// Do nothing right now
	}
	
	
	@Override
	public void init() {
		// TODO Auto-generated method stub
		trace("[RoomController Init]");
		
	}
}
